package com.mxfcode.back.controller;

import com.mxfcode.back.service.CategoryService;
import com.mxfcode.back.service.PermissionService;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.bbs_common.util.AjaxJson;
import com.mxfcode.bbs_common.util.CommUtils;
import com.mxfcode.entity.BBSCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/3/7 23:25
 */
@Controller
@RequestMapping("/back/category")
public class CategoryController {
    private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;

    @PostMapping("categoryList")
    @ResponseBody
    public PageDataResult categoryList(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize){
        logger.info("获取自定义分类列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取服务类目列表
            pdr = categoryService.getCategoryList(pageNum ,pageSize);
            logger.info("权限菜单列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("权限菜单列表查询异常！", e);
        }
        return pdr;
    }
    @GetMapping("allAategoryList")
    @ResponseBody
    public List<BBSCategory> getAllAategoryList(){
        List<BBSCategory> categoryList = categoryService.getAllCategoryList();
        return categoryList;
    }

    @PostMapping("/setCategory")
    @ResponseBody
    public Map<String,Object> setCategory(BBSCategory category){
        logger.info("用户设置自定义分类[新增或更新]！category:" + category);
        Map<String,Object> data = new HashMap();
        if(category.getId() == null){
            //新增权限
            data = categoryService.addCategory(category);
        }else{
            //修改权限
            data = categoryService.updateCategory(category);
        }
        return data;
    }

    @PostMapping("del")
    @ResponseBody
    public Map<String, Object> del(@RequestParam("id") Integer id) {
        logger.info("删除用户自定义分类！id:" + id);
        Map<String, Object> data = new HashMap<>();
        //删除服务类目类型
        data = categoryService.del(id);
        return data;
    }
}
